package com.itheima.info.manage.system.dao;

import com.itheima.info.manage.system.domain.Student;

import java.util.ArrayList;

/*
增
删
改
查


 */
public class OtherStudentDao implements BaseStudentDao {
    private static ArrayList<Student> stus = new ArrayList<>();
    static {
        Student cai = new Student("0001", "小蔡", 28, "1992-01-01");
        Student lu = new Student("0002", "小鹿", 30, "1992-01-01");
        Student wu = new Student("0003", "小吴", 31, "1992-01-01");
        Student huang = new Student("0004", "小黄", 28, "1992-01-01");
       stus.add(cai);
       stus.add(lu);
       stus.add(wu);
       stus.add(huang);


    }
    @Override
    public boolean addStudent(Student student) {
       stus.add(student);
        return true;
    }

    /*
    查询所有的学生
     */
    public Student[] findAllStudents() {
      Student[] students=new Student[stus.size()];
      stus.toArray(students);


      return students;
    }

    public void deleteStudentBySid(String sid) {
        //根据 索引来删除
        int index = getIndex(sid);
        stus.remove(index);

    }

    //根据sid 找索引
    public int getIndex(String sid) {
        //遍历数组
        for (int i = 0; i < stus.size(); i++) {
            Student stu =stus.get(i);
            //判断学号是否相同
            if (stu != null && stu.getSid().equals(sid)) {
                //相同 返回 索引
                return i;
            }
        }
        //所有都不同 返回 -1
        return -1;
    }

    public void updateStudent(String sid, Student newStu) {
        int index = getIndex(sid);
       stus.set(index,newStu);

    }
}
